package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //建立连接
       ConnectionFactory f =  new ConnectionFactory();
       f.setHost("192.168.64.140");
       f.setUsername("admin");
       f.setPassword("admin");

        Channel c = f.newConnection().createChannel();


        //定义队列 ，要求服务器先吧队列准备好

        c.queueDeclare("helloworld", false, false, false, null);


        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("收到:" +msg);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        //从队列接收数据(消费数据)
      //  c.basicConsume("helloworld", true, 处理消息的回调对象，取消消息的回调对象);
        c.basicConsume("helloworld", true,  deliverCallback,cancelCallback);

    }
}
